package com.minibobbo.utilities.particles;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;

public class Particle {
	float size;
	Color c;
	Vector2 loc;
	Vector2 direction;
	Vector2 gravity;
	Texture pixel;

	boolean fade;

	// How long until the particle disappears.
	float duration;
	float totalDuration;

	boolean active;

	public Particle() {
		active = false;
		loc = new Vector2();
		direction = new Vector2();
		gravity = new Vector2();
	}

	public Particle(Color c) {
		super();
		this.c = c;
	}

	/**
	 * Sets the texture region of this particle. It should be set to a single
	 * white pixel for drawing and shading.
	 */
	public void setTextureRegion(Texture pixel) {
		this.pixel = pixel;
	}

	public Particle spawnParticle(float size, Color c, float xLoc, float yLoc,
			float xSpd, float ySpd, float duration) {
		totalDuration = duration;
		fade = true;
		this.size = size;
		this.c = c;
		loc.x = xLoc;
		loc.y = yLoc;
		direction.x = xSpd;
		direction.y = ySpd;
		this.duration = duration;
		active = true;
		return this;
	}

	float alphaValue;
	
	public void update(float delta) {
		if (fade) {
			alphaValue = duration / totalDuration;
		}
		if (active) {
			direction.add(gravity.cpy().mul(delta));
			loc.add(direction.cpy().mul(delta));
			duration -= delta;
			if (duration <= 0)
				active = false;
		}
	}

	public void draw(SpriteBatch sb) {

		if (active) {
			sb.setColor(c.r, c.g, c.b, alphaValue);
			sb.draw(pixel, loc.x - (size / 2), loc.y - (size / 2), size, size);
		}
	}
}
